$(function () {
  //需求1:实现退出功能
  let layer = layui.layer;
  $("#logout").on("click", () => {
    layer.confirm("走了,老弟?", { icon: 3, title: "提示" }, function (index) {
      location.href = "/login.html";
      localStorage.removeItem("token");
      layer.close(index);
    });
  });
  //需求2:渲染用户有头像和用户名
  //要写在退出登录逻辑外,这里要调用它
  getUserInfo();
});
//获取用户信息,渲染用户名和头像功能
function getUserInfo() {
  axios({
    method: "GET",
    url: "/my/userinfo",
    // headers: {
    //   Authorization: localStorage.getItem("token"),
    // },
  }).then((res) => {
    //console.log(res.data);
    //判断
    if (res.data.status !== 0) {
      return layui.layer.msg(res.data.message);
    }

    //渲染用户头像和名称
    renderUserInfo(res.data.data);
  });
}
function renderUserInfo(user) {
  let name = user.nickname || user.username;
  //给欢迎后面赋值
  $(".welcome").html("来了,老弟&nbsp;" + name);
  if (user.user_pic == null) {
    $(".avatar-img").hide();
    $(".avatar-text").show().html(name[0].toUpperCase());
  } else {
    $(".avatar-img").show().attr("src", user.user_pic);
    $(".avatar-text").hide();
  }
}
